﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;
using System.Xml.Linq;
using Novacode;
using Unis.Framework.Core.Infrastructure;
using Unis.Framework.Core.Utility;
using KingRoad.EPM.PersonnelLogBLL.UIRules;
using NPOI.SS.Util;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using BorderStyle = NPOI.SS.UserModel.BorderStyle;
using VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment;
using DotNet.Service;
using KingRoad.EPM.ProjectInfoBLL;
using KingRoad.EPM.PersonnelLogEntity.Model;


namespace KingRoad.EPM.Web.Areas.PersonnelLog.Controllers
{
    public class BaseController : Controller
    {
        public const int ALL_RECORD_ITEMS = 1000000;
        public const int CURRENT_PAGE_NUM = 0;
        /// <summary>
        /// 监理/施工单位信息客户端UIRule
        /// </summary>
        public ProjectUnitUIRule ProjectUnitUIRule
        {
            get { return EngineContext.Current.Resolve<ProjectUnitUIRule>(); }
        }

        /// <summary>
        ///驻地办信息客户端UIRule
        /// </summary>
        public ProjectSupervisingUIRule ProjectSupervisingUIRule
        {
            get { return EngineContext.Current.Resolve<ProjectSupervisingUIRule>(); }
        }

        /// <summary>
        ///总监办信息客户端UIRule
        /// </summary>
        public ProjectSupervisionUIRule ProjectSupervisionUIRule
        {
            get { return EngineContext.Current.Resolve<ProjectSupervisionUIRule>(); }
        }

        /// <summary>
        /// 公共文件UIRule
        /// </summary>
        public CommonFileUIRule CommonFileUIRule
        {
            get { return EngineContext.Current.Resolve<CommonFileUIRule>(); }
        }
        /// <summary>
        ///中心试验室信息客户端UIRule
        /// </summary>
        public ProjectVerifyUIRule ProjectVerifyUIRule
        {
            get { return EngineContext.Current.Resolve<ProjectVerifyUIRule>(); }
        }

        /// <summary>
        /// 安全施工模块数据字典客户端UIRule
        /// </summary>
        public ScmSafetyconstructiondicUIRule ScmSafetyconstructiondicUIRule
        {
            get { return EngineContext.Current.Resolve<ScmSafetyconstructiondicUIRule>(); }
        }

        ///// <summary>
        ///// 安全知识/安全培训客户端UIRule
        ///// </summary>
        //public ScmSafetyknowledgeUIRule ScmSafetyknowledgeUIRule
        //{
        //    get { return EngineContext.Current.Resolve<ScmSafetyknowledgeUIRule>(); }
        //}

        ///// <summary>
        ///// 安全生产台帐客户端UIRule
        ///// </summary>
        //public ScmSafeproductionUIRule ScmSafeproductionUIRule
        //{
        //    get { return EngineContext.Current.Resolve<ScmSafeproductionUIRule>(); }
        //}
        ///// <summary>
        ///// 安全监督台帐客户端UIRule
        ///// </summary>
        //public ScmSafesupervisionUIRule ScmSafesupervisionUIRule
        //{
        //    get { return EngineContext.Current.Resolve<ScmSafesupervisionUIRule>(); }
        //}

        ///// <summary>
        ///// 安全考核客户端UIRule
        ///// </summary>
        //public ScmSafetyassessmentUIRule ScmSafetyassessmentUIRule
        //{
        //    get { return EngineContext.Current.Resolve<ScmSafetyassessmentUIRule>(); }
        //}

        ///// <summary>
        ///// 安全考核考核项目客户端UIRule
        ///// </summary>
        //public ScmAssessmentitemsUIRule ScmAssessmentitemsUIRule
        //{
        //    get { return EngineContext.Current.Resolve<ScmAssessmentitemsUIRule>(); }
        //}

        ///// <summary>
        ///// 被考核单位UIRule
        ///// </summary>
        //public ScmAssessmentunitUIRule ScmAssessmentunitUIRule
        //{
        //    get { return EngineContext.Current.Resolve<ScmAssessmentunitUIRule>(); }
        //}
        ///// <summary>
        ///// 安全考核项目设置及打分信息UIRule
        ///// </summary>
        //public ScmAssessmentscoreUIRule ScmAssessmentscoreUIRule
        //{
        //    get { return EngineContext.Current.Resolve<ScmAssessmentscoreUIRule>(); }
        //}

        /// <summary>
        /// 人员日志管理—施工日志（土建）客户端UIRule
        /// </summary>
        public PlmConstructionlogcivilUIRule PlmConstructionlogcivilUIRule
        {
            get { return EngineContext.Current.Resolve<PlmConstructionlogcivilUIRule>(); }
        }
        ///// <summary>
        ///// 特种设备台账信息UIRule
        ///// </summary>
        //public ScmSpecialequipmentUIRule ScmSpecialequipmentUIRule
        //{
        //    get { return EngineContext.Current.Resolve<ScmSpecialequipmentUIRule>(); }
        //}
        ///// <summary>
        ///// 特种作业人员台账信息UIRule
        ///// </summary>
        //public ScmSpecialpersonnelUIRule ScmSpecialpersonnelUIRule
        //{
        //    get { return EngineContext.Current.Resolve<ScmSpecialpersonnelUIRule>(); }
        //}
        ///// <summary>
        ///// 整体风险评估台账信息UIRule
        ///// </summary>
        //public ScmRiskassessUIRule ScmRiskassessUIRule
        //{
        //    get { return EngineContext.Current.Resolve<ScmRiskassessUIRule>(); }
        //}

        /// <summary>
        /// 日常检查信息UIRule
        /// </summary>
        public ScmDailycheckUIRule ScmDailycheckUIRule
        {
            get { return EngineContext.Current.Resolve<ScmDailycheckUIRule>(); }
        }
        /// <summary>
        /// 人员信息UIRule
        /// </summary>
        public PlmPersonnelinfoUIRule PlmPersonnelinfoUIRule
        {
            get { return EngineContext.Current.Resolve<PlmPersonnelinfoUIRule>(); }
        }  /// <summary>
           /// 人员记录信息UIRule
           /// </summary>
        public PlmPersonnelinforecordUIRule PlmPersonnelinforecordUIRule
        {
            get { return EngineContext.Current.Resolve<PlmPersonnelinforecordUIRule>(); }
        }

        ///// <summary>
        ///// 安全费用信息客户端UIRule
        ///// </summary>
        //public ScmSecurecostUIRule ScmSecurecostUIRule
        //{
        //    get { return EngineContext.Current.Resolve<ScmSecurecostUIRule>(); }
        //}
        public PlmDailysupervisionUIRule PlmDailysupervisionUIRule
        {
            get { return EngineContext.Current.Resolve<PlmDailysupervisionUIRule>(); }
        }
        /// <summary>
        /// 人员变动UIRule
        /// </summary>
        public PlmTurnoverUIRule PlmTurnoverUIRule
        {
            get { return EngineContext.Current.Resolve<PlmTurnoverUIRule>(); }
        }
        /// <summary>
        /// 施工单位标段UIRule
        /// </summary>
        public ProjectSegmentUIRule ProjectSegmentUIRule
        {
            get { return EngineContext.Current.Resolve<ProjectSegmentUIRule>(); }
        }

        /// <summary>
        /// 人员日志管理—检测日志UIRule
        /// </summary>
        public PlmTestlogUIRule PlmTestlogUIRule
        {
            get { return EngineContext.Current.Resolve<PlmTestlogUIRule>(); }
        }

        public PlmDirectorlogUIRule PlmDirectorlogUIRule
        {
            get { return EngineContext.Current.Resolve<PlmDirectorlogUIRule>(); }
        }

        /// <summary>
        /// 审批表UIRule
        /// </summary>
        public ScmAuditUIRule ScmAuditUIRule
        {
            get { return EngineContext.Current.Resolve<ScmAuditUIRule>(); }
        }

        /// <summary>
        /// 人员日志管理—专业监理日志 UIRule
        /// </summary>
        public PlmSpecialtylogUIRule PlmSpecialtylogUIRule
        {
            get { return EngineContext.Current.Resolve<PlmSpecialtylogUIRule>(); }
        }
        /// <summary>
        /// 人员日志管理—专业监理日志子表 UIRule
        /// </summary>
        public PlmSpecialtylogSubUIRule PlmSpecialtylogSubUIRule
        {
            get { return EngineContext.Current.Resolve<PlmSpecialtylogSubUIRule>(); }
        }
        /// <summary>
        /// 人员日志管理—专业监理日志隐蔽子表 UIRule
        /// </summary>
        public PlmSpecialtylogYBSubUIRule PlmSpecialtylogYBSubUIRule
        {
            get { return EngineContext.Current.Resolve<PlmSpecialtylogYBSubUIRule>(); }
        }
        /// <summary>
        /// 人员日志管理—监理员日志 UIRule
        /// </summary>
        public PlmSupervisorlogUIRule PlmSupervisorlogUIRule
        {
            get { return EngineContext.Current.Resolve<PlmSupervisorlogUIRule>(); }
        }
        /// <summary>
        /// 人员日志管理—监理员日志子表 UIRule
        /// </summary>
        public PlmSupervisorlogSubUIRule PlmSupervisorlogSubUIRule
        {
            get { return EngineContext.Current.Resolve<PlmSupervisorlogSubUIRule>(); }
        }
        /// <summary>
        /// 人员日志管理—监理员日志隐蔽子表 UIRule
        /// </summary>
        public PlmSupervisorlogYBSubUIRule PlmSupervisorlogYBSubUIRule
        {
            get { return EngineContext.Current.Resolve<PlmSupervisorlogYBSubUIRule>(); }
        }
        ///// <summary>
        ///// 隐患整改通知书台帐UIRule
        ///// </summary>
        //public ScmPotentialadviceUIRule ScmPotentialadviceUIRule
        //{
        //    get { return EngineContext.Current.Resolve<ScmPotentialadviceUIRule>(); }
        //}
        // /// <summary>
        ///// 三类人员台帐UIRule
        ///// </summary>
        //public ScmThreepersonnelUIRule ScmThreepersonnelUIRule
        //{
        //    get { return EngineContext.Current.Resolve<ScmThreepersonnelUIRule>(); }
        //}
        /// <summary>
        /// 请假管理UIRule
        /// </summary>
        public PlmPersonleaveUIRule PlmPersonleaveUIRule
        {
            get { return EngineContext.Current.Resolve<PlmPersonleaveUIRule>(); }
        }
        /// <summary>
        /// 人员考勤管理UIRule
        /// </summary>
        public PlmPersonnelattendanceUIRule PlmPersonnelattendanceUIRule
        {
            get { return EngineContext.Current.Resolve<PlmPersonnelattendanceUIRule>(); }
        }
        /// <summary>
        /// 人员工资管理UIRule
        /// </summary>
        public PlmPersonnelwagesUIRule PlmPersonnelwagesUIRule
        {
            get { return EngineContext.Current.Resolve<PlmPersonnelwagesUIRule>(); }
        }



        /// <summary>
        /// 当前登录人的ID
        /// </summary>
        public int GetLoginUserId
        {
            get { return Convert.ToInt32(UserService.CurrUserInfo.Id); }
        }
        /// <summary>
        /// 当前登录人的真实姓名
        /// </summary>
        public string GetLoginUserRealName
        {
            get { return UserService.CurrUserInfo.RealName; }
        }
        /// <summary>
        /// 当前登录人的用户名称
        /// </summary>
        public string GetLoginUserName
        {
            get { return UserService.CurrUserInfo.UserName; }
        }
        /// <summary>
        /// 当前登录人的部门编码
        /// </summary>
        public string GetLoginUserDepartmentCode
        {
            get { return UserService.CurrUserInfo.DepartmentCode; }
        }
        /// <summary>
        /// 当前登录人的部门ID
        /// </summary>
        public int GetLoginUserDepartmentId
        {
            get { return UserService.CurrUserInfo.DepartmentId.Value; }
        }
        /// <summary>
        /// 当前登录人的部门名称
        /// </summary>
        public string GetLoginUserDepartmentName
        {
            get { return UserService.CurrUserInfo.DepartmentFullName; }
        }
        /// <summary>
        /// 当前登录人所有基础信息实体
        /// </summary>
        public DotNet.Utilities.BaseUserInfo GetLoginUserInfo
        {
            get { return UserService.CurrUserInfo; }
        }

        /// <summary>
        /// 获取标段ID和标段名称
        /// </summary>
        /// <returns></returns>
        public List<KingRoad.EPM.ProjectInfoEntity.PROJECT_SEGMENT> GetSeglist
        {
            get { return new KingRoad.EPM.ProjectInfoBLL.Project_SegmentBLL().GetNowadayOrglist(); }
        }

        /// <summary>
        /// 获取监理的信息列表
        /// </summary>
        public List<DotNet.Model.MyEntity.Base_Organize> GetOrganizeForJL
        {
            get { return new OrganizeService().GetOrganizeByCode("JL").ToList(); }
        }
        /// <summary>
        /// 获取驻地监理的信息列表
        /// </summary>
        public List<DotNet.Model.MyEntity.Base_Organize> GetOrganizeForZDJL
        {
            get { return new OrganizeService().GetOrganizeByCode("ZDJL").ToList(); }
        }
        /// <summary>
        /// 获取检测单位的信息列表
        /// </summary>
        public List<DotNet.Model.MyEntity.Base_Organize> GetOrganizeForJC
        {
            get { return new OrganizeService().GetOrganizeByCode("JC").ToList(); }
        }
        /// <summary>
        /// 当前登录人所属的组织结构
        /// </summary>
        public DotNet.Model.MyEntity.Base_Organize GetLoginUserOrganizeEntity
        {
            get
            {
                return new OrganizeService().GetOrganizeEntityById(GetLoginUserDepartmentId.ToString());
            }
        }


        /// <summary>
        /// 获取当前登录单位所负责监理,检测,驻地监理的综合信息-名称和标段ID
        /// </summary>
        public List<KingRoad.EPM.ProjectInfoEntity.PROJECT_SEGMENT> Get_JL_JC_ZDJL_SegmentInfo
        {
            get
            {
                var list1 = GetJLProjectSegmentInfo;
                var list2 = GetJCProjectSegmentInfo;
                var list3 = GetZDJLProjectSegmentInfo;
               // var list = list1.Concat(list2).Concat(list3).ToList();



                var list = ConcatList(new List<List<KingRoad.EPM.ProjectInfoEntity.PROJECT_SEGMENT>>
                {
                    list1,list2,list3
                });



                return list;
            }
        }
        /// <summary>
        /// 获取当前登录单位所负责监理-名称和标段ID
        /// </summary>
        public List<KingRoad.EPM.ProjectInfoEntity.PROJECT_SEGMENT> GetJLProjectSegmentInfo
        {
            get
            {
                return new KingRoad.EPM.ProjectInfoBLL.Project_SegmentBLL().GetNowadayOrglist(1);
            }
        }
        /// <summary>
        /// 获取当前登录单位所负责检测-名称和标段ID
        /// </summary>
        public List<KingRoad.EPM.ProjectInfoEntity.PROJECT_SEGMENT> GetJCProjectSegmentInfo
        {
            get
            {
                return new KingRoad.EPM.ProjectInfoBLL.Project_SegmentBLL().GetNowadayOrglist(3);
            }
        }
        /// <summary>
        /// 获取当前登录单位所负责驻地监理-名称和标段ID
        /// </summary>
        public List<KingRoad.EPM.ProjectInfoEntity.PROJECT_SEGMENT> GetZDJLProjectSegmentInfo
        {
            get
            {
                return new KingRoad.EPM.ProjectInfoBLL.Project_SegmentBLL().GetNowadayOrglist(4);
            }
        }
        /// <summary>
        /// 获取当前登录单位所负责施工单位信息
        /// </summary>
        public List<KingRoad.EPM.ProjectInfoEntity.PROJECT_SEGMENT> ShiGong_Segment
        {
            get
            {
                return new Project_HTDWFRelagionBLL().GetlistByUserInfo(1);
            }
        }
        /// <summary>
        /// 获取当前登录单位所负责监理,检测,驻地监理 + 施工单位 的综合信息-名称和标段ID
        /// </summary>
        public List<KingRoad.EPM.ProjectInfoEntity.PROJECT_SEGMENT> Get_JL_JC_ZDJL_SG_SegmentInfo
        {
            get
            {
                var list1 = GetJLProjectSegmentInfo;
                var list2 = GetJCProjectSegmentInfo;
                var list3 = GetZDJLProjectSegmentInfo;
                var list4 = ShiGong_Segment;
                //var list = list1.Concat(list2).Concat(list3).Concat(list4).ToList();


                var list = ConcatList(new List<List<KingRoad.EPM.ProjectInfoEntity.PROJECT_SEGMENT>>
                {
                    list1,list2,list3,list4
                });

                return list;
            }
        }



        private List<T> ConcatList<T>(List<List<T>> list)
        {
            List<T> ret = new List<T>();
            list.ForEach(t =>
            {
                if (t != null && t.Any())
                    ret= ret.Concat(t).ToList();
            });
            return ret;
        }


        /// <summary>
        /// 获取当前登录人所管辖的所有标段合同号集合
        /// </summary>
        /// <returns></returns>
        public string Get_JL_JC_ZDJL_SG_SegmentCodes()
        {
            string codes = "";
            foreach (KingRoad.EPM.ProjectInfoEntity.PROJECT_SEGMENT info in Get_JL_JC_ZDJL_SG_SegmentInfo)
            {
                codes += "'" + info.SEGMENT_CODE + "',";
            }
            codes = codes.Trim(',');
            if (codes == "")
                codes = "''";
            return codes;
        }

        /// <summary>
        /// 根据标段获取单位信息
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public ProjectUnitInfo GetUnitInfoByCode(string code)
        {
            ProjectUnitInfo info = new ProjectUnitInfo();
            List<KingRoad.EPM.ProjectInfoEntity.PROJECT_SEGMENT> list = Get_JL_JC_ZDJL_SG_SegmentInfo.Where(p => p.SEGMENT_CODE == code).ToList();
            if (list.Count > 0)
            {
                info.UNIT_CODE = list[0].SEGMENT_CODE;
                info.UNIT_NAME = list[0].SEGMENT_DEPARTNAME;
                info.UNIT_ID = list[0].SEGMENT_ORGID == null ? 0 : Convert.ToInt32(list[0].SEGMENT_ORGID);
            }
            return info;
        }
        /// <summary>
        /// 根据类型获取标段信息
        /// </summary>
        /// <param name="type">0=监理单位 1=施工单位 2=所有单位</param>
        /// <returns></returns>
        public List<ProjectUnitInfo> GetUnitListByType(int type)
        {
            List<ProjectUnitInfo> list = new List<ProjectUnitInfo>();
            List<KingRoad.EPM.ProjectInfoEntity.PROJECT_SEGMENT> tlist = new List<ProjectInfoEntity.PROJECT_SEGMENT>();
            if (type == 0)
            {
                tlist = Get_JL_JC_ZDJL_SegmentInfo;
            }
            if (type == 1)
            {
                tlist = ShiGong_Segment;
            }
            if (type == 2)
            {
                tlist = Get_JL_JC_ZDJL_SG_SegmentInfo;
            }
            foreach (KingRoad.EPM.ProjectInfoEntity.PROJECT_SEGMENT tinfo in tlist)
            {
                ProjectUnitInfo info = new ProjectUnitInfo();
                info.UNIT_CODE = tinfo.SEGMENT_CODE;
                info.UNIT_NAME = tinfo.SEGMENT_DEPARTNAME;
                info.UNIT_ID = tinfo.SEGMENT_ORGID == null ? 0 : Convert.ToInt32(tinfo.SEGMENT_ORGID);
                list.Add(info);
            }
            return list;
        }
        public List<KingRoad.EPM.ProjectInfoEntity.PROJECT_SEGMENT> JianLi_Segment
        {
            get
            {
                return new Project_HTDWFRelagionBLL().GetlistByUserInfo(2);
            }
        }

        public List<KingRoad.EPM.ProjectInfoEntity.PROJECT_SEGMENT> JianCe_Segment
        {
            get
            {
                return new Project_HTDWFRelagionBLL().GetlistByUserInfo(4);
            }
        }

        public List<KingRoad.EPM.ProjectInfoEntity.PROJECT_SEGMENT> ZhuDiJianLi_Segment
        {
            get
            {
                return new Project_HTDWFRelagionBLL().GetlistByUserInfo(5);
            }
        }

        /// <summary>
        /// 根据URL获取模块ID
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public int GetEntityByUrl(string url)
        {
            return new ModuleService().GetEntityByUrl(url).Id;
        }

        #region Methods

        /// <summary>
        /// 获取RequestForm的值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public T GetRequestFormValue<T>(string key) where T : IConvertible
        {
            if (Request.Form[key] == null) return default(T);
            //{
            //if (typeof(T).Name == "Int32") 
            //}

            return (T)Convert.ChangeType(Request.Form[key], typeof(T));
        }
        /// <summary>
        /// 根据文件路径获取xml的XDocumen对象
        /// </summary>
        /// <param name="xmlpath">xml服务器端路径</param>
        /// <returns></returns>
        public XDocument LinqLoadXmlFromPath(string xmlpath)
        {

            if (String.IsNullOrEmpty(xmlpath))
                throw new NullReferenceException("xml路径为空!");
            if (!System.IO.File.Exists(xmlpath))
                throw new NullReferenceException("该目录下不存在该文件!");
            return XDocument.Load(xmlpath);
        }

        /// <summary>
        /// 设置excel样式
        /// </summary>
        /// <param name="book">工作簿</param>
        /// <param name="align">水平对齐方式（center、left、right）</param>
        /// <param name="border">是否有边框</param>
        /// <param name="size">字体大小</param>
        /// <param name="weight">字体粗细</param>
        /// <returns></returns>
        public ICellStyle Getcellstyle(IWorkbook book, string align = "center", bool border = true, short size = 10, short weight = 100)
        {
            ICellStyle style = book.CreateCellStyle();
            //垂直对齐
            style.VerticalAlignment = VerticalAlignment.Center;
            //自动换行
            style.WrapText = true;
            //水平对齐
            switch (align)
            {
                case "center":
                    style.Alignment = HorizontalAlignment.Center;
                    break;
                case "left":
                    style.Alignment = HorizontalAlignment.Left;
                    break;
                case "right":
                    style.Alignment = HorizontalAlignment.Right;
                    break;
                default:
                    style.Alignment = HorizontalAlignment.Center;
                    break;
            }
            //字体
            IFont font = book.CreateFont();
            font.FontHeightInPoints = size;
            font.Boldweight = weight;
            style.SetFont(font);
            if (border)
            {
                style.BorderTop = BorderStyle.Thin;
                style.BorderBottom = BorderStyle.Thin;
                style.BorderLeft = BorderStyle.Thin;
                style.BorderRight = BorderStyle.Thin;
            }
            return style;
        }
        /// <summary>
        /// 创建单元格
        /// </summary>
        /// <param name="row">行实体</param>
        /// <param name="index">列数</param>
        /// <param name="style">单元格样式</param>
        /// <param name="value">单元格值</param>
        public void CreateCell(IRow row, int column, ICellStyle style, string value)
        {
            ICell cell = row.CreateCell(column);
            cell.CellStyle = style;
            cell.SetCellValue(value);
        }
        /// <summary>
        /// 根据Excel列类型获取列的值
        /// </summary>
        /// <param name="cell">单元格</param>
        /// <returns></returns>
        public string GetCellValue(ICell cell)
        {
            if (cell == null)
                return string.Empty;
            switch (cell.CellType)
            {
                case CellType.Blank:
                    return string.Empty;
                case CellType.Boolean:
                    return cell.BooleanCellValue.ToString();
                case CellType.Error:
                    return cell.ErrorCellValue.ToString();
                case CellType.Numeric:
                    if (DateUtil.IsCellDateFormatted(cell))
                    {
                        return cell.DateCellValue.ToString();
                    }
                    else
                    {
                        return cell.ToString();
                    }
                case CellType.Unknown:
                default:
                    return cell.ToString();
                case CellType.String:
                    return cell.StringCellValue;
                case CellType.Formula:
                    try
                    {
                        HSSFFormulaEvaluator e = new HSSFFormulaEvaluator(cell.Sheet.Workbook);
                        e.EvaluateInCell(cell);
                        return cell.ToString();
                    }
                    catch
                    {
                        return cell.NumericCellValue.ToString();
                    }
            }
        }
        #endregion



        public FileStreamResult ExportToWord<T>(string fileName, T t)
        {
            string path = Server.MapPath("~/Areas/PersonnelLog/Upload/" + fileName);
            string savePath = Server.MapPath("~/Areas/PersonnelLog/Upload/Temp/" + Guid.NewGuid().ToString() + fileName);
            using (DocX docx = DocX.Load(path))
            {
                var rex = new Regex("[0-9]+?");
                docx.Bookmarks.ForEach(f =>
                {
                    var bookMarkName = f.Name;
                    if (rex.IsMatch(bookMarkName))
                    {
                        bookMarkName = f.Name.Substring(0, f.Name.Length - 1);
                    }

                    if (t.ContainProperty(bookMarkName))
                    {
                        var obj = Reflection.GetProperty(t, bookMarkName);
                        var value = obj ?? "";
                        var propertyType = value.GetType().FullName;
                        if (propertyType == "System.DateTime")
                            f.SetText(Convert.ToDateTime(value).ToString("yyyy年MM月dd日"));
                        else
                            f.SetText(value.ToString());
                    }
                });
                docx.SaveAs(savePath);
            }
            fileName = HttpUtility.UrlEncode(fileName, Encoding.GetEncoding("UTF-8"));
            FileStream file = new FileStream(savePath, FileMode.Open);
            return File(file, "application/vnd.ms-word", fileName);
        }

        public DocX GetSonExportInfo<T>(string fileName, T t)
        {
            string path = Server.MapPath("~/Areas/PersonnelLog/Upload/" + fileName);
            using (DocX docx = DocX.Load(path))
            {
                docx.Bookmarks.ForEach(f =>
                {
                    var bookMarkName = f.Name;
                    if (t.ContainProperty(bookMarkName))
                    {
                        var obj = Reflection.GetProperty(t, bookMarkName);
                        var value = obj ?? "";
                        var propertyType = value.GetType().FullName;
                        if (propertyType == "System.DateTime")
                            f.SetText(Convert.ToDateTime(value).ToString("yyyy年MM月dd日"));
                        else
                            f.SetText(value.ToString());
                    }
                });
                return docx.Copy();
            }
        }

        public void DoTest()
        {
            var aa = "";
        }

        protected void CreateAssistId(object uiRule, int assisId, out int ret)
        {
            ret = assisId;

            var objRet = Reflection.InvokeMethodByName(uiRule, "IsExitAssistId", assisId);

            var isExit = Convert.ToBoolean(objRet);
            if (isExit)
            {
                var newAssisId = GenerateRandom();
                ret = newAssisId;
                CreateAssistId(uiRule, newAssisId, out ret);
            }
        }
        //产生随机数，尽量不重复
        public int GenerateRandom(int iMax = 2000000000, int iNum = 10)
        {
            var ret = 0;
            long lTick = DateTime.Now.Ticks;
            List<int> lstRet = new List<int>();
            for (int i = 0; i < iNum; i++)
            {
                Random ran = new Random((int)lTick * i);
                int iTmp = ran.Next(iMax);
                lstRet.Add(iTmp);
                lTick += (new Random((int)lTick).Next(978));
            }

            Random ranIndex = new Random();
            var indexof = ranIndex.Next(0, iNum - 1);

            ret = lstRet[indexof];
            return ret;
        }
        /// <summary>
        /// 保存文件保存方法
        /// </summary>
        /// <param name="BizID"></param>
        /// <param name="BizType"></param>
        /// <param name="FileIds"></param>
        /// <returns></returns>
        public ActionResult BatchAdd(int BizID, string BizType, string FileIds)
        {
            string statuCode = "300";
            string statuMessage = "数据保存失败！";

            // <param name="bizId">业务ID（对应具体表AssisId）</param>
            // <param name="bizType">业务编码</param>
            // <param name="fileIds">文件IDS</param>
            string ids = CommonFileUIRule.BatchAddAttach(BizID, BizType, FileIds);

            if (!String.IsNullOrEmpty(ids))
            {
                statuCode = "200";
                statuMessage = "数据保存成功！";
            }
            return Json(new KingRoad.EPM.Web.ViewModels.CallbackStatus
            {
                statusCode = statuCode,
                message = statuMessage,
                data = ids
            });
        }

        //删除附件操作
        public ActionResult Delete(int id)
        {
            string statuCode;
            string statuMessage;
            var ret = CommonFileUIRule.DeleteCommonFile(id);
            if (ret)
            {
                statuCode = "200";
                statuMessage = "删除成功！";
            }
            else
            {
                statuCode = "300";
                statuMessage = "删除失败，请重试！";
            }
            return Json(new KingRoad.EPM.Web.ViewModels.CallbackStatus
            {
                message = statuMessage,
                statusCode = statuCode
            });
        }
        public void SetSectionValue(string sessionName, object sessionObj)
        {
            Session.Timeout = 600;//设置Session超时时间为600分钟
            Session[sessionName] = sessionObj;
        }

        public T GetSessionValue<T>(string sessionName)
        {
            var sessionObj = Session[sessionName];
            return (T)sessionObj;
        }

    }
}